package com.invengo.module_check.ui.activity.login;


import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.WindowManager;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.invengo.base.router.RouterActivityPath;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.databinding.ActivityLoginBinding;
import com.invengo.module_check.ui.fragment.widget.SelectShelfDialog;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */
@Route(path = RouterActivityPath.Check.PAGER_LOGIN)
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> {
    //ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
    @Override
    public void initParam() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
    @Override
    public void initViewObservable() {
        viewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.pSwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod.setImageResource(R.mipmap.show_psw);
                    binding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod.setImageResource(R.mipmap.show_psw_press);
                    binding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        viewModel.uc.adminEvent.observe(this, aBoolean -> {
            if (adminDialog == null)
                initAdminDialog();
            if (!adminDialog.isShowing())
                adminDialog.show();
        });

    }

    private MaterialDialog adminDialog;

    private void initAdminDialog() {
        adminDialog = new MaterialDialog.Builder(this)
                .title("功能设置界面需要管理员密码")
                //限制输入的长度
                .inputRangeRes(2, 8, R.color.colorPrimary)

                //限制输入类型
                .inputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD)
                .input("请输入管理员密码", null, (dialog, input) -> {
                    if (input.toString().equals("88888888")) {
                        ARouter.getInstance().build(RouterActivityPath.Check.PAGER_COMMUNAL).navigation();
                    } else {
                        ToastUtils.showShort("管理员密码有误");
                    }
                    dialog.dismiss();
                })
                .positiveText("确定")
                .negativeText("取消")
                .canceledOnTouchOutside(false)
                .onNegative((dialog, which) -> {
                    dialog.dismiss();
                })
                .build();
        adminDialog.getInputEditText().setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
    }

}